package com.tao.media.api;

import com.tao.base.model.Result;
import com.tao.media.service.impl.MediaFilesServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * <p>
 * 大文件接口层
 * </p>
 *
 * @author taohongrun
 * @since 2024/11/10
 */
@RestController
@Tag(name = "大文件接口层",description ="用于实现分块传输相关接口")
@RequiredArgsConstructor
public class BigFilesController {

    final MediaFilesServiceImpl mediaFilesService;

    @Operation(description = "文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    @Parameter(name = "fileMd5",description = "通过md5检查",in = ParameterIn.QUERY)
    public Result<Boolean> checkfile(
            @RequestParam("fileMd5") String fileMd5
    ) throws Exception {
        return null;
    }


    @Operation(description = "分块文件上传前检测minio")
    @Parameters( value = {
            @Parameter(name = "fileMd5",description = "通过md5检查",in = ParameterIn.QUERY),
            @Parameter(name = "fileMd5",description = "通过chunk分块序号检查",in = ParameterIn.QUERY)
    }
    )
    @PostMapping("/upload/checkchunk")
    public Result<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") int chunk) throws Exception {
        return null;
    }

    @Operation(description = "上传分块文件")
    @PostMapping(value = "/upload/uploadchunk", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Parameters( value = {
            @Parameter(name = "fileMd5",description = "通过md5检查分层",in = ParameterIn.QUERY),
            @Parameter(name = "chunk",description = "通过chunk检查",in = ParameterIn.QUERY)
    })
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public Result<Boolean> uploadchunk(@RequestPart("file") MultipartFile file,
                                       @RequestParam("fileMd5") String fileMd5,
                                       @RequestParam("chunk") int chunk) throws Exception {

        return Result.success(mediaFilesService.uploadBlockFiles(file,fileMd5,chunk));
    }

    @Operation(description = "合并文件")
    @PostMapping("/upload/mergechunks")
    @Parameters(value = {
            @Parameter(name ="fileMd5",in = ParameterIn.QUERY),
            @Parameter(name ="fileName",in = ParameterIn.QUERY),
            @Parameter(name ="chunkTotal",in = ParameterIn.QUERY)
    })
    public Result mergechunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal) throws Exception {
        mediaFilesService.mergerBlockFiles( fileMd5,fileName,chunkTotal,1232141425L);
        return Result.success();
    }


}
